<?php

	// TITLE: Account Account Model
	// FILE: account/model/account.php
	// AUTHOR: AUTOGEN


	$searchAccount = function() {};
	
	function createAccount($id, $username, $password, $date, $status, $isAdmin, $isOffice, $personId) {
			global $con;
			$sql = 'INSERT INTO account (id, username, password, date, status, is_admin, is_office, person_id) VALUES (' . $id . ', ' . $username . ', ' . $password . ', ' . $date . ', ' . $status . ', ' . $isAdmin . ', ' . $isOffice . ', ' . $personId . ')';
			$this->open();
			$result = mysqli_query($con, $sql);
			$id = ($result) ? mysqli_insert_id($con) : $result;
			$this->close();
			return $id;
		}// end function

	function readAccount($id) {
		global $con;
		$sql = 'SELECT * FROM account WHERE id = ' . $id;
		$this->open();
		$results = mysqli_query($con, $sql);
		$this->close();
		if ($results) {
			$result = mysqli_fetch_array($results);
			$account = new Account();
			$account->setId($result[0]);
			$account->setUsername($result[1]);
			$account->setPassword($result[2]);
			$account->setDate($result[3]);
			$account->setStatus($result[4]);
			$account->setIsAdmin($result[5]);
			$account->setIsOffice($result[6]);
			$account->setPersonId($this->readPerson($result[7]));
		} else {
			$account = false;
		}
		return $account;
	}// end function

	function updateAccount($id, $username, $password, $date, $status, $isAdmin, $isOffice, $personId) {
		global $con;
		$sql = 'UPDATE account SET username = ' . $username . ', password = ' . $password . ', date = ' . $date . ', status = ' . $status . ', is_admin = ' . $isAdmin . ', is_office = ' . $isOffice . ', person_id = ' . $personId . ' WHERE id = ' . $id;
		$this->open();
		$result = mysqli_query($con, $sql);
		$this->close();
		return $result;
	}// end function

	function deleteAccount($id) {
		global $con;
		$sql = 'DELETE FROM account WHERE id = ' . $id;
		$this->open();
		$result = mysqli_query($con, $sql);
		$this->close();
		return $result;
	}// end function

	function listAccount() {
		global $con;
		$sql = 'SELECT * FROM account';
		$this->open();
		$results = mysqli_query($con, $sql);
		$this->close();
		if ($results) {
			$accounts = array();
			while ($result = mysqli_fetch_array($results)) {
				$account = new Account();
				$account->setId($result[0]);
				$account->setUsername($result[1]);
				$account->setPassword($result[2]);
				$account->setDate($result[3]);
				$account->setStatus($result[4]);
				$account->setIsAdmin($result[5]);
				$account->setIsOffice($result[6]);
				$account->setPersonId($this->readPerson($result[7]));
				$accounts[] = $account;
			}// end while
		} else {
			$accounts = false;
		}
		return $accounts;
	}// end function


?>
